﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.IO;

namespace TM_Server
{
    class Sign2Member
    {
        #region 全局变量部分

        //将注册信息传递
        String NickName;
        String Pwd;
        String Age;
        NetworkStream Stream;

        #endregion

        #region 构造函数部分

        public Sign2Member(String NickName , String Pwd , String Age , NetworkStream Stream)
        {
            this.NickName = NickName;
            this.Pwd = Pwd;
            this.Age = Age;
            this.Stream = Stream;
        }

        #endregion

        #region 注册QQ号码

        /*
         * 此方法用来注册QQ号码
         * 并写入相应信息
         * 返回值 String : QQ号码 | 写入成功，False | 写入失败
         */
        public String Sign_It()
        {
            String QQ = "";
            SqlCommand Command;
            String UserId = "";
            SqlConnection Connection = new SqlConnection("Data Source=(local);Initial Catalog=TM_Setting_Infomation;Persist Security Info=True;User ID=sa;Password=songna0917");
            try
            {
                Connection.Open();  //打开数据库
                //随机数据库
                Request2Number Num = new Request2Number();
                QQ = Num.GetNumber().Trim();  //得到结果
                if (QQ != "NO")
                {
                    //开始写入用户数据
                    String Order = "Insert Into TM_User(User_NickName , User_Password , User_Age , User_QQ_Number) ";
                    Order += "Values ('" + this.NickName + "' , '" + this.Pwd + "' , '" + this.Age + "' , " + QQ + ")";
                    Command = new SqlCommand(Order , Connection);
                    Command.ExecuteNonQuery();  //执行
                    //得到用户ID
                    Order = "SELECT User_Id FROM TM_User WHERE User_QQ_Number = " + QQ;
                    Command = new SqlCommand(Order, Connection);
                    UserId = Command.ExecuteScalar().ToString();
                    //开始写入好友分组信息
                    if (UserId != "")
                    {
                        Order = "Insert Into TM_Group(Group_Name , Group_UserId) ";
                        Order += "Values ('我的好友' ," + UserId +")";
                        Command = new SqlCommand(Order, Connection);
                        Command.ExecuteNonQuery();  //执行
                        Order = "Insert Into TM_Group(Group_Name , Group_UserId) ";
                        Order += "Values ('黑名单' ," + UserId + ")";
                        Command = new SqlCommand(Order, Connection);
                        Command.ExecuteNonQuery();  //执行
                        //修改标识，表示该QQ已被注册
                        int state = Num.Set_Signd(QQ);
                        if (state == 1)
                        {
                            //如果标识不成功
                            Stream.Write(Encoding.Default.GetBytes("Sorry"), 0, Encoding.Default.GetBytes(QQ).Length);
                            Stream.Flush();
                        }
                        else
                        {
                            //如果标识成功
                            Stream.Write(Encoding.Default.GetBytes(QQ), 0, Encoding.Default.GetBytes(QQ).Length);
                            Stream.Flush();
                        }
                    }
                    else
                    {
                        Stream.Write(Encoding.Default.GetBytes("Sorry"), 0, Encoding.Default.GetBytes(QQ).Length);
                        Stream.Flush();
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("打开数据库，注册时候错误：" + ex.Message);
            }
            return QQ;
        }

        #endregion

    }
}
 